package com.yy.mydota;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yy.adapter.MyexpandableListAdapter;
import com.yy.bean.Group;
import com.yy.bean.Hero;
import com.yy.database.DBHelperService;
import com.yy.mydota.ArticlesFragment.MyAdapter.ListItemView;
import com.yy.widget.PinnedHeaderExpandableListView;
import com.yy.widget.PinnedHeaderExpandableListView.OnHeaderUpdateListener;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AbsListView.LayoutParams;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;

/**
 * 英雄界面展示
 * @author liyuye
 * @date 2015-5-20 上午11:43:01
 */
public class HerosFragment extends Fragment implements OnChildClickListener,
OnGroupClickListener, OnHeaderUpdateListener{
	private static String TAG = "ResourceFragment";
	private GridView gridview_hero;
	private List<Hero> heroNames;
	
	private DBHelperService ds;
	
	private PinnedHeaderExpandableListView myexpandablelist;
	private MyexpandableListAdapter expandAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fra_heros, container, false);
		myexpandablelist = (PinnedHeaderExpandableListView) view.findViewById(R.id.myexpandablelist);
		
		ds = new DBHelperService(getActivity());
		
		expandAdapter = new MyexpandableListAdapter(getActivity(), getGroupItems(), getChildItems());
		myexpandablelist.setAdapter(expandAdapter);
		
		// 展开所有group
		for (int i = 0, count = myexpandablelist.getCount(); i < count; i++) {
			myexpandablelist.expandGroup(i);
		}
		
		myexpandablelist.setOnChildClickListener(this);
		myexpandablelist.setOnGroupClickListener(null);
		myexpandablelist.setOnHeaderUpdateListener(this);
		
		
		return view;
	}

	private List<Hero> getAllName() {
		heroNames = new ArrayList<Hero>();
		Hero hero;
		Cursor cursor = ds.getAllRecords("HEROS");
		for(int i=0;i<cursor.getCount();i++){
			hero = new Hero();
			cursor.moveToPosition(i);
			hero.setHeroID(cursor.getString(cursor.getColumnIndex("HERO_ID")));
			hero.setName(cursor.getString(cursor.getColumnIndex("HERO_NAME")));
			hero.setImageID(cursor.getInt(cursor.getColumnIndex("H_IMAGE")));
			heroNames.add(hero);
		}
		cursor.close();
		ds.closeDB();
		return heroNames;
	}
	
	/**
	 * 获取组列表
	 * @return
	 */
	private List<Group> getGroupItems(){
		List<Group> groupItems = new ArrayList<Group>();
		Cursor cursor = ds.getCursor("select H_TYPE from HEROS GROUP BY H_TYPE");
		Group group = null;
		for(int i=0;i<cursor.getCount();i++){
			cursor.moveToPosition(i);
			group = new Group();
			group.setTitle(cursor.getString(cursor.getColumnIndex("H_TYPE")));
			groupItems.add(group);
		}
		cursor.close();
		ds.closeDB();
		return groupItems;
	}
	
	/**
	 * 获取子列表
	 * @return
	 */
	private List<List<Hero>> getChildItems(){
		ds = new DBHelperService(getActivity());
		List<List<Hero>> groupList = new ArrayList<List<Hero>>();
		List<Hero> mlist = null;
		Cursor cursor2 = null;
		for(int i=0;i<getGroupItems().size();i++){
			cursor2 = ds.queryRecordByCondtion("HEROS", "WHERE H_TYPE='"+getGroupItems().get(i).getTitle()+"'");
			Hero hero = null;
			mlist = new ArrayList<Hero>();
			for(int j=0;j<cursor2.getCount();j++){
				cursor2.moveToPosition(j);
				hero = new Hero();
				hero.setHeroID(cursor2.getString(cursor2.getColumnIndex("HERO_ID")));
				hero.setName(cursor2.getString(cursor2.getColumnIndex("HERO_NAME")));
				hero.setHeroType(cursor2.getString(cursor2.getColumnIndex("H_TYPE")));
				hero.setImageID(cursor2.getInt(cursor2.getColumnIndex("H_IMAGE")));
				mlist.add(hero);
			}
			groupList.add(mlist);
		}
		cursor2.close();
		ds.closeDB();
		return groupList;
		
	}

	@Override
	public void onResume() {
		super.onResume();

	}

	@Override
	public boolean onGroupClick(ExpandableListView parent, View v,
			int groupPosition, long id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getPinnedHeader() {
		View headerView = getActivity().getLayoutInflater().inflate(R.layout.item_gridview_tag, null);
		headerView.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		return headerView;
	}

	@Override
	public void updatePinnedHeader(View headerView, int firstVisibleGroupPos) {
		Group firstVisibleGroup = (Group) expandAdapter.getGroup(firstVisibleGroupPos);
		TextView textView = (TextView) headerView.findViewById(R.id.texv_gridvTag);
		textView.setText(firstVisibleGroup.getTitle());
	}
}
